package com.lian.hope.controller;

import com.lian.hope.utils.AlisOssUtil;
import com.lian.hope.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/upload")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "上传模块相关接口", description = "上传模块相关接口")
public class UploadController {
    @PostMapping
    @Operation(summary = "上传文件")
    private Result<String> upload(@Parameter(description = "文件") MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString() + "-" + System.currentTimeMillis() + originalFilename.substring(
            originalFilename.lastIndexOf("."));

        String url = AlisOssUtil.uploadFile(filename, file.getInputStream());

        return Result.success(url);
    }

}
